<?php
namespace app\common\controller;

use app\common\controller\BaseController;
use think\Controller;

class HomeBaseController extends BaseController
{
    protected function initialize()
    {
        // 初始化站点数据
        $data = self::initData();
        $this->assign($data);
    }

    // 初始化当前数据
    private function initData()
    {
        $sites = self::getSites();
        if (count($sites) == 0) {
            exit('未设置站点！');
        }
        $site = $defs = []; // default site 默认站点
        $lang = strtolower($this->request->controller());
        foreach ($sites as $key => $val) {
            // 默认站点
            if ($val['site_default'] == '1') {
                $defs = self::getSite($val['site_id']);
            }
            // 当前站点
            if ($val['site_mark'] == $lang) {
                $site = self::getSite($val['site_id']);
                break;
            }
        }

        if (count($site) == 0) {
            $site = $defs;
        }

        $data = [
            'site_id' => $site['site_id'],
            'site_home' => $site['site_home'],
            'site_title' => $site['site_title'],
            'site_keywords' => $site['site_keywords'],
            'site_description' => $site['site_description'],
            'site_jscode' => $site['site_jscode'],
        ];
        // 自定义参数
        $param = self::getSiteContent($site['site_id']);
        foreach ($param as $key => $val) {
            $name = 'site_param_' . $val['content_paramid'];
            $value = $val['content_value'];
            if ($val['content_type'] == 'checkbox') {
                $value = unserialize($value);
            }
            $data[$name] = $value;
        }
        return $data;
    }

    protected function page_404($str = '')
    {
        header('HTTP/1.1 404 Not Found');
        $html = '';
        $html .= '<!DOCTYPE html>' . PHP_EOL;
        $html .= '<html lang="zh_CN">' . PHP_EOL;
        $html .= '<head>' . PHP_EOL;
        $html .= '  <meta charset="UTF-8">' . PHP_EOL;
        $html .= '  <title>未找到页面</title>' . PHP_EOL;
        $html .= '</head>' . PHP_EOL;
        $html .= '<body>' . PHP_EOL;
        $html .= '  <p>404 Not Found!</p>' . PHP_EOL;
        $html .= '  <p>' . $str . '</p>' . PHP_EOL;
        $html .= '</body>' . PHP_EOL;
        $html .= '</html>' . PHP_EOL;
        echo $html;

        //dump($this->request);
    }

}
